import { Component, OnInit } from '@angular/core';
import { AddGooBrandRecommendGoodsComponent } from '../add-goo-brand-recommend-goods.component';
import { ErrRes } from '../../../../../frame/global/err-res';
import { GooBrandRecommendGoods } from '../../goo-brand-recommend-goods.type';
import { Services } from '../../../../../frame/module/services';
import { HttpRequestClient } from '../../../../../frame/http-request-client/http-request-client';
import { UrlRou } from '../../../../../frame';
import { Zero3Alert } from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import { Zero3Enum } from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';
import { FroalaEditor } from '../../../../../frame/global/froala-editor';
import { Service } from 'protobufjs';
import { isNullOrUndefined } from 'util';
import { GooBrandCondition } from '../../../../goods-management/goo-brand/goo-brand.type';
import { GooGoodsCondition } from '../../../goo-goods/goo-goods.type';

@Component({
    selector: 'app-main-add-goo-brand-recommend-goods',
    templateUrl: './main-add-goo-brand-recommend-goods.component.html',
    styleUrls: ['./main-add-goo-brand-recommend-goods.component.css']
})
export class MainAddGooBrandRecommendGoodsComponent implements OnInit {

    froalaEditorOptions = Object.assign(FroalaEditor.options, {});

    errRes = new ErrRes();

    domain = new GooBrandRecommendGoods();

    extend = {
        domain: {
            gooGoodsBrands: {
                items: [],
                nickname: "商品品牌",
                initItems: () => {
                    Services.gooBrand.list(new GooBrandCondition(), {
                        page: 0,
                        size: 999,
                        orders: 'createTime-desc,serNum-asc'
                    }).subscribe(item => {
                        if (!isNullOrUndefined(item) && item.code === 'success') {
                            this.extend.domain.gooGoodsBrands.items = [];
                            item.page.content.forEach(gooBrand => {
                                this.extend.domain.gooGoodsBrands.items.push({ id: gooBrand.uuid, name: gooBrand.serBarndName });
                            });
                        }
                    });
                },
                change: (event) => {
                    if(!isNullOrUndefined(event)){
                    this.extend.domain.gooGoods.items = this.extend.domain.allGooGoods.itemMap.get(event.id);
                    }else{
                        this.extend.domain.gooGoods.items=[]
                    }
                }
            },
            allGooGoods: {
                itemMap: null,
                initItemMap: () => {
                    Services.gooGoods.list(new GooGoodsCondition({}),
                        {
                            page: 0,
                            size: 99999,
                            orders: 'createTime-desc,serNum-asc'
                        }).subscribe(resp => {
                            this.extend.domain.allGooGoods.itemMap = new Map();
                            if (!isNullOrUndefined(resp) && resp.code === 'success') {
                                resp.page.content.forEach(item => {
                                    var tmp = [];
                                    if (item.gooBrand) {
                                        if (isNullOrUndefined(this.extend.domain.allGooGoods.itemMap.get(item.gooBrand.uuid))) {
                                            tmp.push({ "id": item.uuid, "name": item.serGoodsName });
                                            this.extend.domain.allGooGoods.itemMap.set(item.gooBrand.uuid, tmp)
                                        } else {
                                            this.extend.domain.allGooGoods.itemMap.get(item.gooBrand.uuid).push({ "id": item.uuid, "name": item.serGoodsName });
                                        }
                                    }
                                });
                            }
                        })
                }

            },
            gooGoods: {
                nickname: "商品名称",
                items: [],
                change:(event)=>{

                }
            },
            default: () => {
                this.domain.serAudit = Zero3Enum.AUDIT_FLAG_WAIT_SUBMIT;
                this.domain.serUsing = Zero3Enum.ENABLE;
                this.extend.domain.serAudit.nickname = Zero3Enum.AUDIT_FLAG.get(this.domain.serAudit);
            },
            serAudit: {
                nickname: ''
            },
            file: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                        } else {
                            this.extend.domain.file.oss.fileUrl = '';
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            serInputGroupData: {
                icon: fa.faCalendar,
                nickname: '日期',
                value: () => {
                    return this.domain.serInputGroupData;
                },
                focus: (event) => {
                    this.domain.serInputGroupData = event.target.value.toString();
                },
                blur: (event) => {
                }
            },
        },
    };

    func = {
        save: {
            req: (event) => {
                Services.gooBrandRecommendGoods.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.gooBrandRecommendGoods.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public addGooBrandRecommendGoods: AddGooBrandRecommendGoodsComponent) {
        this.addGooBrandRecommendGoods.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
        this.extend.domain.gooGoodsBrands.initItems();
        this.extend.domain.allGooGoods.initItemMap();
    }


}
